package com.gexin.app.controller;

import com.gexin.app.request.category.AddCategoryRequest;
import com.gexin.app.request.category.CategoryDto;
import com.gexin.app.request.category.GetSubCategoriesResponse;
import com.gexin.app.request.question.FetchQuestionListRequest;
import com.gexin.app.request.question.FetchQuestionListResponse;
import com.gexin.app.result.Result;
import com.gexin.app.service.QuestionService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/v1/questions")
public class QuestionController {

    @Resource
    private QuestionService questionService;


    @GetMapping("/fetch")
    public Result<FetchQuestionListResponse> fetch(@RequestParam FetchQuestionListRequest fetchQuestionListRequest) {
        FetchQuestionListResponse response = new FetchQuestionListResponse();
        return new Result<>(response);
    }


    @PostMapping("/add_category")
    public Result addCategory(@RequestBody AddCategoryRequest addCategoryRequest) {
        Boolean res = questionService.addCategory(addCategoryRequest.getName(), addCategoryRequest.getParentId());
        return res ? new Result(Result.CODE_SUCCESS, Result.SUCCESS_MESSAGE) : new Result(Result.CODE_ERROR, Result.ERROR_MESSAGE);
    }

    @GetMapping("/sub_categories")
    public Result<GetSubCategoriesResponse> subCategories(@RequestParam("id") Long id) {
        List<CategoryDto> cates = questionService.getSubCateByParentId(id);
        return new Result<>(new GetSubCategoriesResponse(cates, cates.size()));
    }
}
